<?php

/*
    Search controller allows to search movie and show information.
    Searching is available for all users.
*/

class Controller_Search extends Controller_Template {
    private $user_data;
    private $user_id;
    private $language;
    private $time_offset = 10800; //Time zone adjustment.
    
    public function before() {
        $this->language = Cookie::get((Auth::check() ? 'user' : 'guest') . '_language', 'lv');
        Config::set("language", $this->language);
        Lang::load('main', null, $this->language, true);
        
        parent::before();
        $this->user_data = Auth::instance();
        $this->user_id = $this->user_data->get_user_id();
        
        $this->template->language = $this->language;
        $this->template->genres = Model_Orm_Genre::find('all');
        $this->template->cinema_info = Model_Orm_Cinema::find('first');
    }
    
    //Defining the default view (this is the main view of the website).
    public function action_index() {
        
        //Gathering info about the upcoming movie shows.
        $upcoming_shows = Model_Orm_Show::query()
                ->where('start_time', '>', time() + $this->time_offset)
                ->where('auditorium_id', '>', '1')
                ->order_by('start_time', 'asc')
                ->limit(6)
                ->get();
        
        $view_search_upcoming = View::forge('search/upcoming');
        $view_search_upcoming->set("language", $this->language);
        $view_search_upcoming->set('upcoming_shows', $upcoming_shows);
        $this->template->page_content = $view_search_upcoming;
    }
    
    public function action_detailed() {
        $all_genres = Model_Orm_Genre::load_genres($this->language);

        $view_search_detailed = View::forge('search/detailed');
        $view_search_detailed->set('all_genres', $all_genres);
        
        if(Input::method() == "GET") {
            $date_parts = explode(".", Input::get('date'));
            
            if(count($date_parts) == 3 && checkdate((int)$date_parts[1], (int)$date_parts[0], (int)$date_parts[2])) {
                $time_obj = Date::create_from_string(Input::get('date'), 'd.m.Y');
                $start_timestamp = $time_obj->get_timestamp() - $this->time_offset;
                $end_timestamp = $start_timestamp + 86400; // +24hrs.
                
                $results = DB::query('    
                SELECT DISTINCT shows.id FROM shows
                INNER JOIN movies ON movies.id = shows.movie_id
                INNER JOIN movie_genres ON movie_genres.movie_id = movies.id
                WHERE shows.auditorium_id > 1 AND shows.start_time BETWEEN '. $start_timestamp .' AND '.$end_timestamp.
                    ((int)Input::get('genre') > 0 ? (' AND movie_genres.genre_id = ' . Input::get('genre')) : '') .
                    ' AND     (movies.title_lv LIKE \'%'. Input::get('title') .'%\'
                            OR movies.title_en LIKE \'%'. Input::get('title') .'%\'
                            OR movies.title_original LIKE \'%'. Input::get('title') .'%\')              
                '
                )->execute();
                
                if(count($results) > 0) {
                    $movie_ids = [];
                    $ids_found = $results->as_array();
                    
                    foreach($ids_found as $movie_id) {
                        array_push($movie_ids, (int)$movie_id['id']);
                    }
                    
                    $shows_found = Model_Orm_Show::query()->where('id', 'in', $movie_ids)->get();
                    $view_search_detailed->set('shows_found', $shows_found);
                } else {
                    $shows_found = [];
                    $view_search_detailed->set('shows_found', $shows_found);
                }
            }
        } 
        
        $view_search_detailed->set("language", $this->language);
        $this->template->page_custom_scripts = array('datetimepicker.js');
        $this->template->page_content = $view_search_detailed;
    }
    
    //Searching for movies by movie title.
    public function action_title() {
        if(Input::method() == 'GET') {
            $movie_title = Input::get('phrase');
            
            is_null($movie_title) and Response::redirect('/');

            if(strlen($movie_title) < 3) {
                Response::redirect('/');
            }
            
            //Looking for matches in Latvian, English or original title
            $movies_found = Model_Orm_Movie::query()
                    ->where('title_lv', 'LIKE', '%' . $movie_title . '%')
                    ->or_where('title_en', 'LIKE', '%' . $movie_title . '%')
                    ->or_where('title_original', 'LIKE', '%' . $movie_title . '%')
                    ->get();

            $view_search_title = View::forge('search/title');
            $view_search_title->set("language", $this->language);
            $view_search_title->set('movie_info', $movies_found);
            $this->template->page_content = $view_search_title;   
        } else {
            Response::redirect('/');
        } 
    }
}
